<?php
// debug-urls.php
define('WP_USE_THEMES', false);
require_once('wp-load.php');

// 获取当前新闻详情页的URL
$test_url = isset($_GET['url']) ? $_GET['url'] : '';

echo "<h2>URL调试工具</h2>";

if ($test_url) {
    echo "<p>测试URL: " . htmlspecialchars($test_url) . "</p>";
    
    // 显示所有变体
    $variants = [
        '原始' => $test_url,
        'urlencode' => urlencode($test_url),
        'urldecode' => urldecode($test_url),
        'rawurlencode' => rawurlencode($test_url),
        'rawurldecode' => rawurldecode($test_url)
    ];
    
    echo "<h3>URL变体:</h3>";
    foreach ($variants as $name => $variant) {
        echo "<p><strong>{$name}:</strong> " . htmlspecialchars($variant) . "</p>";
    }
    
    // 连接到数据库检查匹配
    $db_config = [
        'host' => 'localhost',
        'user' => 'wordpress',
        'password' => '9f172735e1037be2',
        'database' => 'wordpress',
        'table' => 'NEWSLIST'
    ];
    
    $conn = new mysqli($db_config['host'], $db_config['user'], $db_config['password'], $db_config['database']);
    
    if ($conn->connect_error) {
        die("数据库连接失败: " . $conn->connect_error);
    }
    
    echo "<h3>数据库匹配结果:</h3>";
    
    foreach ($variants as $name => $variant) {
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM NEWSLIST WHERE url = ?");
        $stmt->bind_param("s", $variant);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        
        echo "<p>{$name} 匹配: " . $row['count'] . " 条记录</p>";
        $stmt->close();
    }
    
    $conn->close();
    
} else {
    echo "<form method='get'>
        <input type='text' name='url' placeholder='输入新闻URL' style='width: 500px;'>
        <input type='submit' value='调试'>
    </form>";
}
?>